package com.chinatsp.onlineapp.ui.main_menu;

import java.util.ArrayList;
import java.util.List;

import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.T;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.BaseActivity;
import com.chinatsp.onlineapp.base.Config;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.bean.LimitNum;
import com.chinatsp.onlineapp.bean.ReverseGeocod;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean.ForecastConditionsBean;
import com.chinatsp.onlineapp.presenter.LimitNumDao;
import com.chinatsp.onlineapp.presenter.NewsListDao;
import com.chinatsp.onlineapp.presenter.ReverseGeocodeDao;
import com.chinatsp.onlineapp.presenter.WeatherServiceDao;
import com.chinatsp.onlineapp.ui.limit_rule.LimitNumActivity;
import com.chinatsp.onlineapp.ui.news.NewsMainActivity;
import com.chinatsp.onlineapp.ui.violation.QueryViolationActivity;
import com.chinatsp.onlineapp.ui.weather.WeatherSinglePageActivity;
import com.chinatsp.onlineapp.utils.ActivityUtil;
import com.chinatsp.onlineapp.utils.LocationUtil;

public class MainActivity extends BaseActivity implements View.OnClickListener {

	private RelativeLayout rlQueryViolation;
	private RelativeLayout rlTodayLimit;
	private RelativeLayout rlNews;
	private LinearLayout llWeather;
	private ListView lv;
	private ImageView ivBack;
	private ImageView ivWeatherShow;

	private WeatherListAdapter adapter;
	private List<ForecastConditionsBean> forecastConditions;
	private WeatherServiceDao mWeatherServiceDao;
	private NewsListDao mNewsListDao;
	private LimitNumDao mLimitNumDao;
    private ReverseGeocodeDao mReverseGeocodeDao;
	private TextView tv_limit_number;
	@Override
	public void onRequestSuccess(int requestCode) {
		super.onRequestSuccess(requestCode);
		// 开始更新UI
		showProgress(false);
		switch (requestCode) {
		case Config.CODE_WEATHER_SERVICE:// 更新天气数据
			forecastConditions = mWeatherServiceDao.getmWeatherService().getResp_data().getForecast_conditions();

			// String time =
			// weatherServiceDao.getmWeatherService().getResp_data()
			// .getRelease_time();
			// LogUtil.v(forecastConditions.size() + ""+
			// DateUtil.getWeekSF(DateUtil.changeStringToDate(time)));
			adapter.setData(forecastConditions);

			AnimationDrawable spinner = (AnimationDrawable) ivWeatherShow.getBackground();
			spinner.start();
			
			break;
		case Config.CODE_NEWS_LIST:// 更新新闻数据

			break;
		case Config.CODE_LIMIT_NUM:// 更新限号数据
			// 更新界面数据


			break;
		case Config.CODE_REVERS_GEOCODE:// 定位根据经纬度获取城市
			// 更新界面数据
			ReverseGeocod reverseGeocod = mReverseGeocodeDao.getReverseGeocod();
			
			// 获取天气数据
//			mWeatherServiceDao.getWeatherService(reverseGeocod.getResp_data().getCity_name());
			// 获取限号数据
//			mLimitNumDao.getLimitNum(reverseGeocod.getResp_data().getCity_name());
			break;
		}

	}

	@Override
	public void onRequestFailed(String errorNo, String errorMessage) {
		super.onRequestFailed(errorNo, errorMessage);
		// 开始更新UI
		showProgress(false);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		getLacation();

	}

	private void getLacation() {
		// 注册广播
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.LOCATION_ACTION);
		this.registerReceiver(new LocationBroadcastReceiver(), filter);

		// 启动服务
		Intent intent = new Intent();
		intent.setClass(this, LocationUtil.class);
		startService(intent);
		


	}

	/**
	 * 初始化控件、数据
	 */
	private void initView() {
		rlQueryViolation = (RelativeLayout) findViewById(R.id.rl_query_violation);
		rlTodayLimit = (RelativeLayout) findViewById(R.id.rl_today_limit);
		rlNews = (RelativeLayout) findViewById(R.id.rl_news);
		llWeather = (LinearLayout) findViewById(R.id.ll_weather);
		lv = (ListView) findViewById(R.id.lv_weather_list);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		ivWeatherShow = (ImageView) findViewById(R.id.iv_weather_show);
		tv_limit_number = (TextView) findViewById(R.id.tv_limit_number);
		
		forecastConditions = new ArrayList<ForecastConditionsBean>();
		adapter = new WeatherListAdapter(this, forecastConditions);
		lv.setAdapter(adapter);

		mNewsListDao = new NewsListDao(this, this);
		mWeatherServiceDao = new WeatherServiceDao(this, this);
		mLimitNumDao = new LimitNumDao(this, this);
		mReverseGeocodeDao = new ReverseGeocodeDao(this, this);

		showProgress(true);
		
		
		
		// 获取新闻数据
		mNewsListDao.getNewsList();

		ivBack.setOnClickListener(this);
		rlQueryViolation.setOnClickListener(this);
		rlTodayLimit.setOnClickListener(this);
		rlNews.setOnClickListener(this);
		llWeather.setOnClickListener(this);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				intentWeatherMessage();
			}

		});
		// 获取天气
//		mWeatherServiceDao.getmWeatherService();
		
		
		// 今日限号
//		mLimitNumDao.getLimitNum();
		


	}

	/**
	 * 进入天气界面
	 */
	private void intentWeatherMessage() {

		Bundle bundle = new Bundle();
		bundle.putString("CITY_NAME", "北京");
		ActivityUtil.intentActivity(MainActivity.this, WeatherSinglePageActivity.class, bundle);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.rl_today_limit:
			ActivityUtil.intentActivity(MainActivity.this, LimitNumActivity.class, null);
			break;
		case R.id.rl_query_violation:
			ActivityUtil.intentActivity(MainActivity.this, QueryViolationActivity.class, null);
			break;
		case R.id.rl_news:
			ActivityUtil.intentActivity(MainActivity.this, NewsMainActivity.class, null);
			break;
		case R.id.ll_weather:
			intentWeatherMessage();
			break;
		}

	}

	private class LocationBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (!intent.getAction().equals(Constant.LOCATION_ACTION)) return;
			String latitude = intent.getStringExtra(Constant.LOCATION_LATITUDE);
			String longitude = intent.getStringExtra(Constant.LOCATION_LONGITUDE);
			
			// 根据经纬度获取城市名称
			mReverseGeocodeDao.getReverseGeocode(latitude, longitude);
			MainActivity.this.unregisterReceiver(this);// 不需要时注销
		}
	}		
	
	
}
